#!/usr/bin/env python3
import os
import re
import sys
import gzip
# pip3 install toml
import toml
import subprocess

print("🍀 tge.rs - terminal game engine by rust...")
if len(sys.argv) !=2:
    print("🚀 USAGE: tools/creat_game <GameName>")
    sys.exit(1)

NAME = sys.argv[1]
UPNAME = NAME.upper()
LONAME = NAME.lower()
CAPNAME = NAME.capitalize()

print("🍀 update Cargo.toml add workspace member...")
ct = toml.loads(open("Cargo.toml").read())
ct["workspace"]["members"].append("games/%s" % NAME)
toml.dump(ct, open("Cargo.toml", "w"))

print("🍀 creat assets folder...%s" % ("assets/" + NAME + "/"))
os.system("cp -r assets/snake/ assets/%s/" % NAME)

print("🍀 creat games folder...%s" % ("games/" + NAME +"/"))
os.chdir("tmp")
os.system("rm -fr tge_game_template")
os.system("cp -r ../games/snake/ ./tge_game_template")
for root, dirs, files in os.walk("tge_game_template"):
    for f in files:
        fn = os.path.join(root, f)
        # print(fn)
        fhr = open(fn, 'r')
        fcontent = fhr.read()
        fhr.close()
        fcontent = fcontent.replace("Snake", CAPNAME)
        fcontent = fcontent.replace("SNAKE", UPNAME)
        fcontent = fcontent.replace("snake", LONAME)
        fhw = open(fn, 'w')
        fhw.write(fcontent)
        fhw.close()
os.system("mv tge_game_template ../games/%s"%NAME)

print("🍀 compile & run: \n   cargo run --bin %s\n   cargo run --bin %s --features sdl" % (NAME, NAME))

